% Solve for the Ramsey problem with HSV tax

load tau_grid
load T_grid
welfare = Ramsey_plus_affine_without_epsilon_grid(pr,w,pai,pw,G, T_grid, tau_grid);
load lambda
load tau
load Tr
load c
load y
r_hsv_affine.lambda  = lambda;
r_hsv_affine.tau     = tau;
r_hsv_affine.c       = c;
r_hsv_affine.y       = y;
r_hsv_affine.output  = sum(pai.*y);
r_hsv_affine.welfare = sum(pw.*pai.*Utility(c,y,w,pr));
r_hsv_affine.MTR     = 1-pr.Om./c.^(-pr.gam).*w.^-(1+pr.sig).*y.^pr.sig;
r_hsv_affine.ATR     = (y-c)./y;
r_hsv_affine.WelfareGain = 100*fzero(@(x) sum(pw.*pai.*Utility((1+x).*baseline.c,baseline.y,w,pr))-r_hsv_affine.welfare,.5);
r_hsv_affine.OutputLoss  = 100*(r_hsv_affine.output/baseline.output-1);
r_hsv_affine.EfAveMTR  = sum(pai.*r_hsv_affine.MTR.*r_hsv_affine.y/r_hsv_affine.output);
[temp.y,temp.i] = min(r_hsv_affine.y);
temp.c          = r_hsv_affine.c(temp.i);
r_hsv_affine.Tr2Y      = (temp.c-temp.y)./r_hsv_affine.output;
r_hsv_affine.TrG2Y     = (temp.c-temp.y+G)./r_hsv_affine.output;
r_hsv_affine.Tr        = Tr;

save ./variables/r_hsv_affine r_hsv_affine
delete lambda.mat tau.mat c.mat y.mat tau_grid.mat T_grid.mat G.mat Tr.mat